package com.fingard.dsp.bank.directbank.bocom13;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

/**
 * 交通银行资金清算平台单笔代付
 *
 * 参考文档:
 * 		1、交通银行资金清算平台联机交易接口V1.0.doc
 * 		2、交通银行资金清算平台数据字典V1.0.doc
 *
 * @author Cruise
 * @date 2019/4/18
 */
public class BOCOM13SinglePay extends BOCOM13Base implements ISinglePay {
	@Override
	public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
		RetAtsSglDetail tmpRetDetail = d_190802(p_req1908);
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	private RetAtsSglDetail d_190802(ReqSingle p_req1908) throws Exception {
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
		tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
		try {
			BOCOM13Header tmpBankHeader = new BOCOM13Header();
			tmpBankHeader.transCode = BOCOM13TransCode.singlePay_1908_190802;
			tmpBankHeader.reqSeqID = p_req1908.singleDetail.askNumber;
			String corpNo = getBankConfig().getMapValueByKey("corpNoMapping", p_req1908.singleDetail.payAct);
			if (StringHelper.isNullOrEmpty(corpNo)) {
				tmpRetDetail.respCode = "0";
				tmpRetDetail.respInfo = "未找到账号[" + p_req1908.singleDetail.payAct + "]保险方ID映射,请检查BOCOM13/config.xml中'corpNoMapping'节点配置";
				tmpRetDetail.transState = PaymentState.FAIL_Payment;
				tmpRetDetail.payInfoCode = FailCode.E9981;
				return tmpRetDetail;
			}
			tmpBankHeader.corpNo = corpNo;
			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHeader(xmlWriter, tmpBankHeader);
			xmlWriter.writeStartElement("Body");
			xmlWriter.writeElementString("BusDate", Format.Date8Format.format(new Date()));/* 保险方业务日期 */
			xmlWriter.writeElementString("SeqNo", tmpBankHeader.reqSeqID);/* 保险方流水号 */
			xmlWriter.writeElementString("ApplyNo", p_req1908.singleDetail.oriSourceNoteCode);/* 投保单号 */
			xmlWriter.writeElementString("PolNo", p_req1908.singleDetail.insuPolicyNum);/* 保单号 */
			xmlWriter.writeElementString("Name", p_req1908.singleDetail.recName);/* 客户姓名 */
			xmlWriter.writeElementString("IdType", convertCertTypeToBank(p_req1908.singleDetail.certType));/* 证件类型 */
			xmlWriter.writeElementString("IdNo", p_req1908.singleDetail.certNum);/* 证件号码 */
			xmlWriter.writeElementString("Telephone", p_req1908.singleDetail.cellPhone);/* 手机号码 */
			String isPrivate = "";
			if ("1".equals(p_req1908.singleDetail.isPrivate)) {
				isPrivate = "P";
			} else {
				isPrivate = "E";
			}
			xmlWriter.writeElementString("EpFlag", isPrivate);/* 公私标志(E-对公，P-对私) */
			xmlWriter.writeElementString("PrdName", "");/* 险种名称 */
			String sameBnk = p_req1908.singleDetail.sameBnk.equals("1")  ? "1" : "2";
			xmlWriter.writeElementString("BkFlag", sameBnk);/* 银行标志(1-交通银行,2-非交通银行) */
			xmlWriter.writeElementString("BkNo", p_req1908.singleDetail.cnapsCode);/* 收款行行号 */
			xmlWriter.writeElementString("ActNo", p_req1908.singleDetail.recAct);/* 账号 */
			xmlWriter.writeElementString("CurType", "CNY");/* 交易币种 */
			xmlWriter.writeElementString("Amt", p_req1908.singleDetail.payAmount);/* 交易金额 */
			String businessType = getBankConfig().getSetValue("businessType", "Memo", "1908");
			if (StringHelper.hasAnyChar(businessType)) {
				businessType = getBankConfig().getMapValueByKey("payBusiCode", p_req1908.singleDetail.srcXElement.getSingleText(businessType));
			}
			if (StringHelper.isNullOrEmpty(businessType)) {
				businessType = "C30";// 默认为保险理赔
			}
			xmlWriter.writeElementString("BusType", businessType);/* 收付种类:2.42收付种类*/
			xmlWriter.writeElementString("AgreementNo", p_req1908.singleDetail.authCode);/* 代收协议编号 */
			xmlWriter.writeElementString("Ext001", "");
			xmlWriter.writeElementString("Ext002", "");
			xmlWriter.writeElementString("Ext003", "");
			xmlWriter.writeElementString("Ext004", "");
			xmlWriter.writeElementString("Ext005", "");
			xmlWriter.writeElementString("Ext006", "");
			xmlWriter.writeElementString("Ext007", "");
			xmlWriter.writeElementString("Ext008", "");
			xmlWriter.writeElementString("Ext009", "");
			xmlWriter.writeElementString("Ext010", "");
			xmlWriter.writeEndDocument();
			String[] tmpRetStr = sendToBank(xmlWriter.toXmlString(), bankFront.serverPort);
			if (tmpRetStr[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr[1], tmpSbMsg);
				if (tmpBkReader != null) {
					tmpRetDetail.respCode = "0";
					tmpRetDetail.respInfo = tmpBkReader.getFirstText("/Rsp/Head/ErrMsg");
					String tmpMsgType = tmpBkReader.getFirstText("/Rsp/Head/MsgType");
					String tmpErrCode = tmpBkReader.getFirstText("/Rsp/Head/ErrCode");
					String transState = tmpBkReader.getFirstText("/Rsp/Body/BusStat");
					String tmpRepMsg=tmpBkReader.getFirstText("/Rsp/Body/RepMsg");
					if ("N".equals(tmpMsgType) && "000000".equals(tmpErrCode)&&"1".equals(transState)) {
						// 交易状态:0-处理中 1-成功  2-失败 3-超时
						tmpRetDetail.transState = "D+" + transState;/* 交易状态 */
						tmpRetDetail.payInfoCode = tmpErrCode;
						if (StringHelper.hasAnyChar(tmpRepMsg)) {
							tmpRetDetail.respInfo = tmpRepMsg;
						}
					} else if("E".equals(tmpMsgType) &&"2".equals(transState)){
						tmpRetDetail.transState = "D+"+transState;/* 交易状态 */
					}else {
						tmpRetDetail.respCode = "-1";
					}
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = tmpRetStr[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		return tmpRetDetail;
	}

	@Override
	public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
		tmpRetDetail.reqSeqID = p_req1944.askNumber;
		try {
			BOCOM13Header tmpBankHeader = new BOCOM13Header();
			tmpBankHeader.transCode = BOCOM13TransCode.singlePay_1944_190802;
			tmpBankHeader.reqSeqID = p_req1944.askNumber;
			String corpNo = getBankConfig().getMapValueByKey("corpNoMapping", p_req1944.payAct);
			if (StringHelper.isNullOrEmpty(corpNo)) {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = "未找到账号[" + p_req1944.payAct + "]保险方ID映射,请检查BOCOM13/config.xml中'corpNoMapping'节点配置";
				retAts.writeRetDetail(tmpRetDetail);
				return retAts;
			}
			tmpBankHeader.corpNo = corpNo;
			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHeader(xmlWriter, tmpBankHeader);
			xmlWriter.writeStartElement("Body");
			xmlWriter.writeElementString("BusDate", Format.Date8Format.format(p_req1944.getBgnDateTime()));/* 保险方业务日期 */
			xmlWriter.writeElementString("SeqNo", tmpBankHeader.reqSeqID);/* 保险方原流水号 */
			xmlWriter.writeEndDocument();
			String[] tmpRetStr = sendToBank(xmlWriter.toXmlString(), bankFront.serverPort);
			if (tmpRetStr[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr[1], tmpSbMsg);
				if (tmpBkReader != null) {
					tmpRetDetail.respCode = "0";
					String tmpErrMsg=tmpBkReader.getFirstText("/Rsp/Head/ErrMsg");
					tmpRetDetail.respInfo = tmpErrMsg;
					String tmpMsgType = tmpBkReader.getFirstText("/Rsp/Head/MsgType");
					String tmpErrCode = tmpBkReader.getFirstText("/Rsp/Head/ErrCode");
					String tmpBusStat=tmpBkReader.getFirstText("/Rsp/Body/BusStat");
					tmpRetDetail.payInfoCode = tmpBkReader.getFirstText("/Rsp/Body/RepCode");
					String tmpRepMsg=tmpBkReader.getFirstText("/Rsp/Body/RepMsg");
					if ("N".equals(tmpMsgType) && "000000".equals(tmpErrCode)) {
						// 交易状态:0-处理中 1-成功  2-失败 3-超时
						tmpRetDetail.transState = "D+" + tmpBusStat;/* 交易状态 */
					}else if(tmpErrMsg.contains("交易记录不存在")){
						tmpRetDetail.transState = "D+2";
					}else if(StringHelper.hasAnyChar(tmpBusStat)){
						tmpRetDetail.transState = "D+" + tmpBusStat;
					} else {
						tmpRetDetail.respCode = "-1";
					}
					if (StringHelper.hasAnyChar(tmpRepMsg)) {
						tmpRetDetail.respInfo = tmpRepMsg;
					}
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = tmpRetStr[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	@Override
	public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
		return null;
	}

	@Override
	public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
		return null;
	}
}
